$(function () {

    $(".Tabs .log").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        $(".regBox").css({ display: "none" });
        $(".logBox").css({ display: "flex" });
    })
    $(".Tabs .reg").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        $(".regBox").css({ display: "flex" });
        $(".logBox").css({ display: "none" });
    })


    var isPwdOk = false;
    var isPhoneOk = false;
    var isCodeOk = false;
    var isAgrOk = false;

    $("#createCode").text(randCode());
    $("#createCode").click(function () {
        $("#createCode").text(randCode());
    })

    // $("#user").blur(function () {
    //     isUserOk = false;
    //     var user = $(this).val().trim();
    //     // console.log(user);

    //     var userReg = /^[a-zA-Z_$][\w$]{5,11}$/;

    //     if (userReg.test(user)) {
    //         // $("#userSpan").text("√").css({ color: "green" });
    //         // isUserOk = true;
    //         isExistUser({ user }).then(data => {
    //             var { status, msg } = data;
    //             if (status) {
    //                 $("#userSpan").text("√").css({ color: "green" });
    //                 isUserOk = true;
    //             } else {
    //                 $("#userSpan").text("* 用户名已存在!").css({ color: "red" });
    //             }
    //         }).catch(err => {
    //             throw err;
    //         })
    //     } else {
    //         $("#userSpan").text("* 用户名由数字,大小写字母,下划线,$组成,6-12位,且不能以数字开头").css({ color: "red" });
    //     }
    // })

    $("#pwd").blur(function () {
        isPwdOk = false;
        var pwd = $(this).val();
        // console.log(pwd);

        var pwdReg = /^[\w$]{6,12}$/;

        if (pwdReg.test(pwd)) {
            // $("#pwdSpan").text("√");

            var isExistNum = false;
            var isExistSmall = false;
            var isExistBig = false;
            var isExistSpe = false;

            if (/[0-9]/.test(pwd)) isExistNum = true;
            if (/[a-z]/.test(pwd)) isExistSmall = true;
            if (/[A-Z]/.test(pwd)) isExistBig = true;
            if (/[_$]/.test(pwd)) isExistSpe = true;

            var sum = isExistNum + isExistSmall + isExistBig + isExistSpe;
            switch (sum) {
                case 1:
                    $("#pwdSpan").text("密码强度:弱").css({ color: "green" });
                    break;
                case 2:
                    $("#pwdSpan").text("密码强度:中").css({ color: "yellow" });
                    break;
                case 3:
                    $("#pwdSpan").text("密码强度:强").css({ color: "orange" });
                    break;
                case 4:
                    $("#pwdSpan").text("密码强度:超强").css({ color: "gold" });
                    break;
            }
            isPwdOk = true;
        } else {
            $("#pwdSpan").text("* 密码由数字,大小写字母,下划线,$组成,6-12位").css({ color: "red" });
        }
    })

    $("#phone").blur(function () {
        isPhoneOk = false;
        var phone = $(this).val().trim();
        // console.log(phone);

        var phoneReg = /^1[3-9]\d{9}$/;

        if (phoneReg.test(phone)) {
            // $("#phoneSpan").text("√").css({ color: "green" });
            // isPhoneOk = true;

            isExistPhone({ phone }).then(data => {
                var { status, msg } = data;
                if (status) {
                    $("#phoneSpan").text("√").css({ color: "green" });
                    isPhoneOk = true;
                } else {
                    $("#phoneSpan").text("* 手机号已注册!").css({ color: "red" });
                }
            }).catch(function (err) {
                throw err;
            })
        } else {
            $("#phoneSpan").text("* 手机号码格式不正确").css({ color: "red" });
        }
    })

    $("#email").blur(function () {
        isEmailOk = false;
        var email = $(this).val().trim();
        // console.log(email);

        var emailReg = /^\w+@\w+\.(com|cn|email)$/;

        if (emailReg.test(email)) {
            // $("#emailSpan").text("√").css({ color: "green" });
            // isEmailOk = true;

            isExistEmail({ email }).then(data => {
                var { status, msg } = data;
                if (status) {
                    $("#emailSpan").text("√").css({ color: "green" });
                    isEmailOk = true;
                } else {
                    $("#emailSpan").text("* 邮箱已注册!").css({ color: "red" });
                }
            }).catch(function (err) {
                throw err;
            })
        } else {
            $("#emailSpan").text("* 邮箱格式有误").css({ color: "red" });
        }
    })

    $("#code").blur(function () {
        isCodeOk = false;
        var code = $(this).val().trim();
        // console.log(code);

        var RCode = $("#createCode").text(); //随机的验证码
        var codeReg = new RegExp(`^${RCode}$`, "i");

        if (codeReg.test(code)) {
            $("#codeSpan").text("√").css({ color: "green" });
            isCodeOk = true;
        } else {
            $("#codeSpan").text("* 验证码有误").css({ color: "red" });
        }
    })

    var isRegisterSuccess = false; // 记录是否注册成功
    $(".regBtn").click(function () {
        if (isRegisterSuccess) return false;

        if ($("#agr").is(":checked")) {
            isAgrOk = true;
        } else {
            isAgrOk = false;
            alert("请先阅读并同意协议");
        }

        if (isUserOk && isPwdOk && isPhoneOk && isEmailOk && isCodeOk && isAgrOk) {
            var user = $("#user").val();
            var pwd = $("#pwd").val();
            var phone = $("#phone").val();
            var email = $("#email").val();
            // console.log(user, pwd, phone, email);

            register({ user, pwd, phone, email }).then(data => {
                var { status, msg } = data;
                if (status) {
                    isRegisterSuccess = true;
                    alert("注册成功");
                    $(".regBox").css({ display: "none" })
                    $(".logBox").css({ display: "flex" })
                    $(".log").addClass("active").siblings().removeClass("active");
                } else {
                    alert(msg);
                    isRegisterSuccess = false;
                }
            }).catch(err => {
                throw err;
            });

        } else {
            isRegisterSuccess = false;
        }
    })




    $(".retBtn").click(function () {
        location.href = "../index.html";
    })

    $(".logBtn").click(function () {
        // location.href = "../index.html";
        var user = $("#loguser").val()
        var pwd = $("#logpwd").val()
        console.log(user, pwd);
        if (user && pwd) {
            login({ user, pwd }).then(data => {
                var { status, msg } = data;
                if (status) {
                    if ($("#isRem").is(":checked")) {
                        setCookie("lgc", user, 31);
                    } else {
                        setCookie("lgc", user);
                    }
                    alert("登陆成功");
                    location.href = "./index1.html";
                } else {
                    alert(msg);
                }
            }).catch(function (err) {
                throw err;
            })
        } else {
            alert("请输入完整信息");
        }
    })
})